//
//  OYCategory.m
//  ObjectiveYouTube
//
//  Created by Tristan O'Tierney on 3/15/08.
//  Copyright 2008 Tristan O'Tierney. All rights reserved.
//

#import <ObjectiveYouTube/ObjectiveYouTube.h>

NSString *kOYCategoryURL = @"http://gdata.youtube.com/schemas/2007/categories.cat";
static NSString *kOYCategoryNodeName = @"category";
static NSString *kOYCategoryTermName = @"term";
static NSString *kOYCategoryLabelName = @"label";

@implementation OYCategory
#pragma mark -
#pragma mark Initializers
#pragma mark -

- (id)initWithTerm:(NSString *)term andLabel:(NSString *)label {
	if ((self = [super init])) {
		[self setTerm:term];
		[self setLabel:label];
	}
	
	return self;
}

- (id)initWithXMLElement:(NSXMLElement *)node {
	if ((self = [super init])) {
		if ([[node localName] isEqualToString:kOYCategoryNodeName]) {
			NSXMLNode *termNode = [node attributeForName:kOYCategoryTermName];
			NSXMLNode *labelNode = [node attributeForName:kOYCategoryLabelName];

			[self setTerm:[termNode stringValue]];
			[self setLabel:[labelNode stringValue]];
		} else {
			[self release];
			self = nil;
		}
	}
	
	return self;
}

- (void)dealloc {
	[_term release];
	_term = nil;
	[_label release];
	_label = nil;
	
	[super dealloc];
}

#pragma mark -
#pragma mark Accessors/Mutators
#pragma mark -

- (NSString *)term {
	return _term;
}

- (void)setTerm:(NSString *)term {
	[term retain];
	[_term release];
	_term = term;
}

- (NSString *)label {
	return _label;
}

- (void)setLabel:(NSString *)label {
	[label retain];
	[_label release];
	_label = label;
}

- (NSComparisonResult)compareLabel:(OYCategory *)category {
	return [_label compare:[category label]];
}

- (NSString *)description {
	return _label;
}

- (BOOL)isEqual:(id)obj {
	BOOL equal = NO;
	
	if ([obj isKindOfClass:[OYCategory class]]) {
		equal = [[(OYCategory *)obj term] isEqualToString:_term];
	}
	
	return equal;
}
@end
